<?php

namespace App\Models\Traits;

trait CreateTagsHelper
{
    // 通过 '标签1,标签2,标签3' 的形式创建多个标签
    // 返回 [1,2,3,4,5]
    public function createByName($names)
    {
        $tags = [];
        $names = trim($names, ',');
        $name = explode(',', $names);

        foreach ($name as $val) {
            if(!empty($val)) $tags[] = $this->firstOrCreate(['name' => $val]);
        }
        return collect($tags)->pluck('id')->toArray();
    }
}
